package com.mgl.movesprite.game.model;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.mgl.movesprite.EntitySpriteLevelBase;
import com.mgl.movesprite.IEntityLevel;
import com.mgl.movesprite.game.factory.GameObjectFactory;
import com.mgl.movesprite.game.factory.GameTextureManager;
import com.mgl.movesprite.game.factory.GameType;
import com.mgl.util.Path;

public class Wave extends EntitySpriteLevelBase {

	private Queue<IEntityLevel> monsterList;

	private Path path;

	private float contTime = 0;
	private float timeToAddMonster = 2;

	private GameObjectFactory gameObjectFactory = null;

	public Wave(float pX, float pY, ITextureRegion pTextureRegion,
			VertexBufferObjectManager pVertexBufferObjectManager,
			GameObjectFactory gameObjectFactory) {

		super(pX, pY, pTextureRegion, pVertexBufferObjectManager,
				gameObjectFactory);

		this.gameObjectFactory = gameObjectFactory;

		monsterList = new LinkedList<IEntityLevel>();

	}

	public void init(Path path) {

		this.path = path.clone();
		initWave();

	}

	private void initWave() {
		try {

			for (int i = 0; i < 10; i++) {
				// Monster monster = new Monster(this.getX(), this.getY(),
				// gameTextureManager.getMonsterTexture(),
				// getVertexBufferObjectManager(), gameTextureManager);
				Monster monster = (Monster) gameObjectFactory.createObject(
						GameType.MONSTER, this.getX(), this.getY());
				monster.setPath(path.clone());
				
				monsterList.add(monster);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void update(float pSecondsElapsed, Level level) {

		if (monsterList.isEmpty()) {
			level.removeEntity(this);
		}
		contTime += pSecondsElapsed;
		if (contTime > timeToAddMonster) {

			contTime = 0;
			IEntityLevel monster =  monsterList.poll();

			level.addEntity(monster);

		}

	}

}
